package daemon

import (
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"runtime"

	"github.com/zh-five/xdaemon"
)

func Daemonize(d *bool) {
	var currentDir string
	currentDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
	if err != nil {
		fmt.Println("get current working directory failed!")
		return
	}
	sysType := runtime.GOOS
	if sysType != "windows" {
		//d := flag.Bool("d", false, "Daemon is running")
		flag.Parse()
		if *d {
			// AppDaemon = true
			fmt.Println("daemon run auto")
			//创建一个Daemon对象
			logFile := filepath.Join(currentDir, "daemon.log")
			d := xdaemon.NewDaemon(logFile)
			//调整一些运行参数(可选)
			d.MaxCount = 2 //最大重启次数
			//执行守护进程模式
			d.Run()
		}
	}
}
